package com.jike.mahjong.controller;

import com.jike.mahjong.common.Constants;
import com.jike.mahjong.common.Page;
import com.jike.mahjong.common.util.HttpUtil;
import com.jike.mahjong.model.CoinRecordModel;
import com.jike.mahjong.model.Player;
import com.jike.mahjong.service.PlayerService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by star on 2016/10/9.
 */
@Controller
public class PlayerController {

    private Logger log = Logger.getLogger(PlayerController.class);

    @Autowired
    private PlayerService playerService;

    /**
     * 获取所有玩家列表
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/player/queryPlayerList.html", method = {RequestMethod.GET,
            RequestMethod.POST})
    protected String queryPlayerList(HttpServletRequest request, HttpServletResponse response, Player player, Model model) throws Exception {

        Map<String, Object> para = new HashMap<String, Object>();
        Page page=new Page();
        String currentPage = HttpUtil.getHttpParameter(request, "pageIndex");



        if(!"".equals(currentPage)&&currentPage!=null){
            page.setCurrentPage(Integer.parseInt(currentPage));

        }else{
            page.setCurrentPage(1);
        }

        para.put("playerId", player.getId());
        page=getPlayerList(para,page);
        model.addAttribute("page",page);
        model.addAttribute("playerId",player.getId());
        Map<String, Object> countMap = new HashMap<String, Object>();
        int allCount=playerService.countPlayerByCondition(countMap);
        countMap.put("online",Constants.PLAYER_ONLINE);
        int onlineCount=playerService.countPlayerByCondition(countMap);
        model.addAttribute("allCount",allCount);
        model.addAttribute("onlineCount",onlineCount);

        return "views/player/playerInfo";
    }



    /**
     * 加钻
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/player/coinAdd.html", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> coinAdd(HttpServletRequest request, HttpServletResponse response,  Model model) {
        Map<String, Object> paraMap = new HashMap<String, Object>();
        String addCoin=HttpUtil.getHttpParameter(request,"mahjongCoin");
        String amount=HttpUtil.getHttpParameter(request,"amount");
        String id=HttpUtil.getHttpParameter(request,"id");
        String managerId = request.getSession().getAttribute("m_user_id").toString();
        paraMap.put("id",id);
        paraMap.put("amount",amount);
        paraMap.put("addCoin",addCoin);
        paraMap.put("managerId",managerId);
        String message="";
        int effectRow = 0;
            try {
                effectRow = playerService.addCoin(paraMap,id,addCoin);
                message="充值成功！！";
            } catch (Exception e) {
                message="充值失败！！";
                e.printStackTrace();
            }
        if(effectRow>0){
            Player player=playerService.queryPlayInfoById(paraMap);
            paraMap.put("nowCoin",player.getDiamond());
        }
        paraMap.put("resultMessage", message);
        paraMap.put("effectRow", effectRow);
        return paraMap;
    }


    public Page getPlayerList(Map<String, Object> para,Page  page) {

        page = playerService.queryPlayerInfo(para,page);
        return page;
    }

    /**
     * 查充值记录
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(value = "/player/queryRechargeRecord.html", method = {RequestMethod.GET, RequestMethod.POST})
    public String queryRechargeRecord(HttpServletRequest request, HttpServletResponse response,  Model model) {
        Map<String, Object> paramMap =  HttpUtil.paramsToMap(request.getParameterMap());
        Page page=new Page();
        String currentPage = HttpUtil.getHttpParameter(request, "pageIndex");
        String role= (String) paramMap.get("playerRole");

        if(!"".equals(currentPage)&&currentPage!=null){
            page.setCurrentPage(Integer.parseInt(currentPage));

        }else{
            page.setCurrentPage(1);
        }


        if(role==null||"".equals(role)){
             role=Constants.PLAYER_ROLE+"";
            paramMap.put("playerRole",role);
        }

        if("1".equals(role)){
            page=playerService.queryPlayerRechargeRecord(paramMap,page);
        }else{
            page=playerService.queryManagerRechargeRecord(paramMap,page);
        }
        model.addAttribute("page",page);
        model.addAttribute("paramMap",paramMap);
        return "views/player/playerRechargeRecord";
    }

    /**
     * 查询玩家个人信息根据玩家账号
     * @param params
     * @return
     */
    @RequestMapping(value = "/player/agentQueryPlayInfoById.html", method = RequestMethod.POST)
    @ResponseBody
    public Object agentQueryPlayInfoById(@RequestBody Map<String, Object> params,HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> mapResult = new HashMap<String,Object>();
        String playerId = params.get("playerId").toString();
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("id", playerId);
        map.put("playerId", playerId);
        Player player = playerService.queryPlayInfoById(map);
        mapResult.put("errorCode", "0");
        mapResult.put("playerInfo", player);
        return mapResult;

    }


    /**
     * 设置为管理员
     * @param request
     * @param response
     * @param player
     * @param model
     * @return
     */
    @RequestMapping(value = "/player/settingPlayerRole.html", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> settingPlayerRole(HttpServletRequest request, HttpServletResponse response, Player player, Model model) {
        int efficetRow = 0;
        Map<String, Object> para = new HashMap<String, Object>();
        String playerId = HttpUtil.getHttpParameter(request, "id");
        String role = HttpUtil.getHttpParameter(request, "role");
        if (!playerId.equals("")) {
            para.put("id", playerId);
            para.put("role", role);
            if(role.equals("1")){
                para.put("serverUrl", Constants.AGENT_URL);
            }else{
                para.put("serverUrl","");
            }

            efficetRow = playerService.setManagerRole(para);
        }
        para.put("sucessRow", efficetRow);
        return para;

    }


    /**
     * 修改用户状态
     *
     * @param request
     * @param response
     * @param player
     * @param model
     * @return
     */
    @RequestMapping(value = "/player/updatePlayerInfo.html", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> updatePlayerInfo(HttpServletRequest request, HttpServletResponse response, Player player, Model model) {
        int efficetRow = 0;
        Map<String, Object> para = new HashMap<String, Object>();
        String playerId = HttpUtil.getHttpParameter(request, "playerId");
        String playerState = HttpUtil.getHttpParameter(request, "playerState");
        if (!playerId.equals("")) {
            efficetRow = playerService.updatePlayerState(playerId,playerState);
        }
        para.put("sucessRow", efficetRow);
        return para;
    }


}
